package cn.pconline.officialclub.web.wap;

import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.Result;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.autoclub.web.UserAuth;
import cn.pconline.autoclub.web.wap.ActionKit;
import cn.pconline.officialclub.entity.OActivity;
import cn.pconline.officialclub.entity.OActivityMember;
import cn.pconline.officialclub.entity.OClub;
import cn.pconline.officialclub.entity.OCommonMember;
import cn.pconline.officialclub.service.OActivityService;
import cn.pconline.officialclub.service.OClubService;
import cn.pconline.officialclub.service.OMemberService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author xulin
 */
@Controller
@RequestMapping(value="/wap/brand/activity")
public class OActivityAction {
    
    @Autowired
    private OClubService oClubService;
    @Autowired
    private OActivityService oActivityService;
    @Autowired
    private OMemberService oMemberService;
    @Autowired
    private UserAuth  userAuth;

    /**
     * 活动页面列表
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/activityList.do",method= RequestMethod.GET)
    public String activityList(HttpServletRequest request,HttpServletResponse response){
        long oClubId = WebUtils.paramOClubId(request);
        OClub oClub = oClubService.findValidClub(oClubId);
        if(oClub == null){
            return ActionKit.SNED_ERROR_404;
        }
        request.setAttribute("oClub", oClub);
        return "template/official/wap/activity/activityList";
    }
    /**
     * 分页获取活动列表
     * @param request
     * @param response 
     */
    @RequestMapping(value="/listActivityListByClubId.do",method= RequestMethod.GET)
    public  void listActivityListByClubId(HttpServletRequest request,HttpServletResponse response){
        WebUtils.setEncoding(request, response);
        long oClubId = WebUtils.paramOClubId(request);
        int pageNo = WebUtils.paramInt(request, "pageNo", ActionKit.DEF_PAGE_NUM);
        int pageSize = WebUtils.paramInt(request, "pageSize", pageNo, ActionKit.MIN_PAGE_SIZE,ActionKit.MAX_PAGE_SIZE);
        String callback = WebUtils.param(request, "callback", "");
        boolean isXss = true;
        OClub oClub = oClubService.findValidClub(oClubId);
        if(oClub == null){
            ActionKit.printErrorMsg(response, "该品牌车友会不存在", callback);
            return;
        }
        User user = userAuth.getUser(request, response);
        long userId = user.getUid();
        OCommonMember member = null;
        if(userId>0){
            member = oMemberService.findCommonMember(userId, oClubId);
        }
        Pager<OActivity> pager = oActivityService.pageActivityBy(userId, oClubId, false, pageNo, pageSize);
        JSONObject obj = new JSONObject();
        JSONArray array = new JSONArray();
        if(pager!=null){
             if (pager.getResultList() != null && !pager.getResultList().isEmpty()) {
                for (OActivity oActivity : pager.getResultList()) {
                    JSONObject jsonContent = new JSONObject();
                    jsonContent.put("activityId", oActivity.getActivityId());
                    jsonContent.put("title", EncodeUtils.encodeForHTML(oActivity.getTitle(), isXss));
                    jsonContent.put("signupDate", oActivity.getSignupDate().getTime());
                    jsonContent.put("beginDate", oActivity.getBeginDate().getTime());
                    jsonContent.put("endDate", oActivity.getEndDate().getTime());
                    jsonContent.put("pic", EncodeUtils.encodeForHTML(oActivity.getPicUrl160x120(), isXss));
                    jsonContent.put("limitCount", oActivity.getMaxJoin());
                    Result result = oActivityService.getActivityStatusDesc(userId, oActivity);
                    jsonContent.put("statusCode", result.getString("activityCode"));
                    jsonContent.put("statusMsg", result.getString("activityMsg"));
                    jsonContent.put("place", oActivity.getOnlineStatus() == OActivity.IS_ONLINE?"线上":oActivity.getProvinceName()+"-"+oActivity.getCityName());
                    array.add(jsonContent);
                }
            }
        }
        obj.put("data", array);
        ActionKit.putPagerMessage(obj, pager);
        ActionKit.printSuccessMsg(response, obj, callback);
    }
    /**
     * 
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/activityDetail.do",method= RequestMethod.GET)
    public String activityDetail(HttpServletRequest request,HttpServletResponse response){
        long activityId = WebUtils.paramLong(request, "activityId", 0);
        long oClubId = WebUtils.paramOClubId(request);
        OClub oClub = oClubService.findValidClub(oClubId);
        if(oClub == null){
            return ActionKit.SNED_ERROR_404;
        }    
        OActivity oActivity = oActivityService.findValidActivity(activityId);
        if(oActivity == null){
            return ActionKit.SNED_ERROR_404;
        }
        User user = userAuth.getUser(request, response);
        OCommonMember member = null;
        if(user.getUid()>0){
            member = oMemberService.findCommonMember(user.getUid(), oClubId);
        }
        //完整内容
        if (oActivity != null) {
            oActivity.setFullContent(oActivityService.getActivityContent(activityId));
            oActivity.setUserActivityStatus(oActivityService.getActivityStatusDesc(user.getUid(),oActivity).getString("activityCode"));
        }
        //该活动参加的人员列表
        List<OActivityMember> listMember = oActivityService.listMember(activityId, OActivityMember.STATUS_PASS);
        List<OActivityMember> listApply = oActivityService.listActivityMember4Audit(activityId);
        //活动日记
//      Pager<ActivityDiary> listDiary = activityService.pageActivityDiary(0,clubId,0,pageNo, pageSize);
        request.setAttribute("address", oActivity.getActivityAddress());
        request.setAttribute("userStatus", oActivity.getUserActivityStatus());
        request.setAttribute("user", user);
        request.setAttribute("oClub", oClub);
        request.setAttribute("member", member);
        request.setAttribute("clubId", oActivity);
        request.setAttribute("activity", oActivity);
        request.setAttribute("listMember", listMember);
        request.setAttribute("listApply", listApply);
        request.setAttribute("oActivity", oActivity);
        return "template/official/wap/activity/activityDetail";
    }
}
